<?php
/**
    
 * @author jboatws
 * @copyright 2011
 * 
 * 
 * function checkEmail() {
var email = document.getElementById('emailaddress');
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!filter.test(email.value)) {
alert('Please provide a valid email address');
email.focus;
return false;
}
}

Read more: http://www.marketingtechblog.com/programming/javascript-regex-emailaddress/#ixzz1PoMYBrZT

 * */
class defined_Validation {
    private $rules;
    private $message;
    private $error;
    function __construct($rules= array(),$message= array()){
         $this->rules = $rules;
         $this->message = $message;
         $this->error = array();
    }
    function __destruct(){
        unset ($this->rules);
        unset ($this->message);
        unset ($this->error);
    }
    function __set($key,$value){
        $this->$key = $value;
    }
    function __get($key){
        return $this->$key;
    }
	function addRules($name, $rules){
		$this->rules[$name] = $rules;
	}	
    function showErrors(){var_dump($this->error);}
    function required($data, $req=true){
        if ($data!='')
            return $req;
        return !$req;                    
    }
    function req($data, $req=true){
        return $this->required($data, $req);                    
    }
    function email($email, $req=true){
        return preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/", $email)?$req:!$req;
    }
    function equalTo($data, $data2){
        return ($data==$data2);
    }
    function min($data,$min){
        return (intval($data)>=$min);
        
    }
    function max($data,$max){
        return (intval($data)<=$max);
        
    }
    function range ($data, $range){
		if (is_string ($range) )
			$range = explode (',', $range );
		if(is_array($range))
			list ($min, $max) = $range;			
		else
			list ($min, $max) = array(0,strlen($data));
        return ($this->min($data,$min) && $this->max($data,$max));
    }
    function lenght($data, $len){
        return (strlen(trim($data))==$len);
    }
    function len($data, $len){
        return $this->lenght($data, $len);
    }
    function minLenght($data, $min){
        return $this->min(strlen(trim($data)),$min);
        
    }
    function minLen($data, $min){
        return $this->minLenght($data, $min);
        
    }
    function maxLenght($data, $max){
        return $this->max(strlen(trim($data)),$max);
        
    }
    function maxLen($data, $max){
        return $this->maxLenght($data, $max);
        
    }
    function number($data, $req=true){
        return is_numeric($data)?$req:!$req;
    }
    function num($data, $req=true){
        return $this->number($data, $req);
    }
    function url($data, $req=true){
        return preg_match ('/^http(s)?:\/\/[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(\/.*)?$/i', $data)?$req:!$req;
    }
    function date($data,  $format='d-m-Y'){ 
                
      if (preg_match ("/^([0-9]{1,2})-([0-9]{1,2})-([0-9]{4})$/", $data, $parts))
      {
        //check weather the date is valid of not

    	if(checkdate($parts[2],$parts[1],$parts[3]))
    	   return true;  
      }
      return false;
    } // date VI-VN
    function datetime($data,  $format='d-m-Y'){return $this->date($data,  $format);} //datetime VI-VN
    function digits($data, $req=true){return $this->number($data, $req);}
    function phone($data, $req=true){
       return $this->mobile($data, $req);
    } //VI-VN
    function mobile($data, $format='d/m/Y'){
         return preg_match("/^((\(\+?84\)[\-\.\s]?)|(\+?84[\-\.\s]?)|(0))((\d{3}[\-\.\s]?\d{6})|(\d{2}[\-\.\s]?\d{8}))$/", $data)?$req:!$req;
    } //VI-VN
    function expression($data,$pattern){
        return preg_match ($pattern, $data);
    }
    function isarray($data, $req=true){return (is_array($data)?$req:!$req);}
    
    function validate($data_array = array()){ return $this->validata($data_array);}
        
    function validata($data_array = array()){
       
        if (empty ($data_array))return 'none';
        foreach ($this->rules as $key=>$rule){
            if(is_array($rule)){
                foreach ($rule as $k=>$row){                    
                    //$return = call_user_method_array($k,$this,array($data_array[$key],$row));
                    $return = $this->$k($data_array[$key],$row);
                    if ($return===false){
                        return $key;
                        //more or else
                    }
                        
                }
                
            }else{//string:required, email => rule is true
                //$return = call_user_method($rule,$this,$data_array[$key]);
                $return = $this->$rule($data_array[$key],true);
                if ($return===false){
                    return $key;
                    //more or else
                }
                        
            }
        }
        return true;
    }
    function getMessage($code/*field:rule*/){}
//    function getMessage($field,$rule){
//        return $this->message[$field][$rule];
//    }
}